package 力扣._57_插入区间;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

public class Solution {
    public int[][] insert(int[][] intervals, int[] newInterval) {
        List<int[]> list = new LinkedList<>();
        List<int[]> result = new LinkedList<>();
        int i;
        for (int[] interval : intervals) {
            list.add(new int[]{interval[0], interval[1]});
        }
        for (i = 0; i < list.size() && newInterval[0] > list.get(i)[0]; i++) ;
        list.add(i, newInterval);
        for (int[] ints : list) {
            if (result.size() == 0)
                result.add(ints);
            else {
                //无交集
                if (result.get(result.size() - 1)[1] < ints[0])
                    result.add(ints);
                else {  //有交集
                    result.get(result.size() - 1)[1] = Math.max(result.get(result.size() - 1)[1], ints[1]);
                }
            }
        }
        return result.toArray(new int[result.size()][]);

    }

    public static void main(String[] args) {
        int[][] insert = new Solution().insert(new int[][]{{1, 3}, {6, 9}}, new int[]{2, 5});
        System.out.println(Arrays.stream(insert).toList().toString());
    }
}
